package com.jyu.hakkatranslation.controller;

import com.google.gson.Gson;
import com.jyu.hakkatranslation.mapper.AnswerMapper;
import com.jyu.hakkatranslation.pojo.Answer;
import com.jyu.hakkatranslation.vo.CommitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("users")
public class MuseumController {

    @Autowired
    AnswerMapper answerMapper;
    @PostMapping("/commit")
    public String UserCommit(@RequestBody String  commit){
        Gson gson = new Gson();
        CommitVo commitVo = gson.fromJson(commit, CommitVo.class);
        String context = commitVo.getUserAnswer();
        String src = commitVo.getMp3Id();
        Answer answer = new Answer();
        answer.setMp3Id(src.charAt(5)-'0');///mp3/1.mp3
        answer.setUserAnswer(context);
        answerMapper.insertUserAnswer(answer);
        String commit_successfully = gson.toJson("Commit Successfully");
        return commit_successfully;
    }
    @GetMapping("/SelectAll")
    public String SelectAll(){
        List<Answer> answers = answerMapper.SelectAll();
        String returnString="音乐id,用户答案\n";
        for (Answer answer : answers) {
            returnString+=answer.getMp3Id()+",";
            returnString+=answer.getUserAnswer();
            returnString+="\n";
        }
        System.out.println(returnString);
        return returnString;
    }
}
